//
//  LFBNetworkingTool.m
//  LFBLoveFreshBee
//
//  Created by 张睿 on 16/8/20.
//  Copyright © 2016年 丁鼎. All rights reserved.
//

#import "LFBNetworkingTool.h"

@implementation LFBNetworkingTool


/** 单例 实例化 */
+ (instancetype)sharedTool{
    
    static LFBNetworkingTool *instance;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        //1.实例化
        
        instance = [[LFBNetworkingTool alloc] initWithSessionConfiguration:configuration];
        
        //1.2 添加根地址
        [instance setValue:[NSURL URLWithString:@"http://iosapi.itcast.cn/loveBeen/"] forKey:@"baseURL"];
        
        //2.可接受格式
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
        
        //3.请求格式（默认：二进制）
        instance.requestSerializer = [AFJSONRequestSerializer serializer];
        
        //4.超时
        instance.requestSerializer.timeoutInterval = 10.0;
        
    });
    
    return instance;
}




/**
 *  请求首页数据
 */
-(void)getdataWithRequestTpye:(LFBRequestType) type WithSuccessBlock:(SuccessCompleteBlock)success OrFailureBlock:(FaileCompleteBlock)failure{
    
    NSString *url  = [NSString string];
    
    switch (type) {
        case LFBRequestTypeHome:
            url = @"focus.json.php";
            break;
        case LFBRequestTypeFirstSell:
            url = @"firstSell.json.php";
            break;
        case LFBRequestTypeAD:
            url = @"ad.json.php";
            break;
        case LFBRequestTypeSuperMarket:
            url = @"supermarket.json.php";
            break;
        case LFBRequestTypeSearch:
            url = @"search.json.php";
            break;
        case LFBRequestTypePromotion:
            url = @"promotion.json.php";
            break;
        case LFBRequestTypeMyorders:
            url = @"MyOrders.json.php";
            break;
        case LFBRequestTypeMyCoupon:
            url = @"MyCoupon.json.php";
            break;
        case LFBRequestTypeSystemMessage:
            url = @"SystemMessage.json.php";
            break;
        case LFBRequestTypeUserMessage:
            url = @"UserMessage.json.php";
            break;
        case LFBRequestTypeMyAdress:
            url = @"MyAdress.json.php";
            break;
            
        default:
            break;
    }
    
    NSDictionary *dict =@{@"call" : @(type)};
    
    [self sendRequestWith:url andParameters:dict success:^(id responseObject) {
        success(responseObject);
    } failure:^(NSError *error) {
        failure(error);
    }];
    
}

-(void)sendRequestWith:(NSString *)url andParameters:(NSDictionary *)parameters success:(SuccessCompleteBlock)success failure:(FaileCompleteBlock)failure{
    
    LFBNetworkingTool *tools = [LFBNetworkingTool sharedTool];
    
    
    [tools POST:url parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
    }];
    
}




@end
